import time
import smbus2 as smbus
 
bus = smbus.SMBus(1)  # 1 indicates /dev/i2c-1
 
# SHT20 address, from datasheet
SHT20_ADDR = 0x40
 
def read_temperature(addr=SHT20_ADDR):
    bus.write_byte(addr, 0xF3)  # 0xF3 is the command for temperature measurement
    time.sleep(0.1)  # wait for the sensor to measure (from datasheet)
    high_byte = bus.read_byte(addr)  # read the high byte
    low_byte = bus.read_byte(addr)  # read the low byte
    temperature = (high_byte << 8 | low_byte) * 175.72 / 65536.0 - 46.85  # calculate temperature
    return temperature
 
def read_humidity(addr=SHT20_ADDR):
    bus.write_byte(addr, 0xF5)  # 0xF5 is the command for humidity measurement
    time.sleep(0.1)  # wait for the sensor to measure (from datasheet)
    high_byte = bus.read_byte(addr)  # read the high byte
    low_byte = bus.read_byte(addr)  # read the low byte
    humidity = (high_byte << 8 | low_byte) * 125.0 / 65536.0 - 6.0  # calculate humidity
    return humidity
 
# Example usage
# while True:
#     print("温度: %.2f C" % read_temperature())
#     print("湿度: %.2f %%" % read_humidity())
#     print("\n")
#     time.sleep(1)

print("温度: %.2f C" % read_temperature())
print("湿度: %.2f %%" % read_humidity())